#pragma once

#include "message.hxx"
#include "icsearch.hpp"
#include "es_factory.hpp"
#include <json/json.h>

class ESMessage
{
public:
    using ptr = std::shared_ptr<ESMessage>;
    ESMessage(const std::shared_ptr<elasticlient::Client> &es_client) : _es_client(es_client) {}

    ~ESMessage() {}

    bool createIndex()
    {
        bool ret = ESIndex(_es_client, "message")
                       .appendFields("user_id", "keyword", "standard", false)
                       .appendFields("message_id", "keyword", "standard", false)
                       .appendFields("create_time", "long", "standard", false)
                       .appendFields("chat_session_id", "keyword", "standard", true)
                       .appendFields("content")
                       .ceeateIndex();

        if (ret == false)
        {
            LOG_ERROR("消息信息检索失败");
            return false;
        }

        LOG_INFO("消息信息检索成功");

        return true;
    }

    bool appendData(const std::string &user_id,
                    const std::string &message_id,
                    const long create_time,
                    const std::string &chat_session_id,
                    const std::string &content)
    {
            bool ret = ESInsert(_es_client, "message")
                           .appendFields("message_id", message_id)
                           .appendFields("create_time", create_time)
                           .appendFields("user_id", user_id)
                           .appendFields("chat_session_id", chat_session_id)
                           .appendFields("content", content)
                           .insert(message_id);

            if (ret == false)
            {
                LOG_ERROR("数据插入失败");
                return false;
            }

            LOG_INFO("数据插入成功");
            return true; 
    }

        std::vector<Message> search(const std::string &key, const std::string &ssid)
        {
            std::vector<Message> res;
            Json::Value json_user = ESSearch(_es_client, "message")
                                    .append_must_term("chat_session_id.keyword", ssid)
                                    .append_must_match("content", key)
                                    .search();

            if (json_user.isArray() == false)
            {
                LOG_ERROR("用户搜索结果为空，或者结果不是数组类型");
                return res;
            }

            int sz = json_user.size();
            for (int i = 0; i < sz; i++)
            {
                Message message;
                message.set_user_id(json_user[i]["_source"]["user_id"].asString());
                message.set_message_id(json_user[i]["_source"]["message_id"].asString());
                boost::posix_time::ptime ctime(boost::posix_time::from_time_t(
                    json_user[i]["_source"]["create_time"].asInt64()));
                message.set_create_time(ctime);
                message.set_session_id(json_user[i]["_source"]["chat_session_id"].asString());
                message.set_content(json_user[i]["_source"]["content"].asString());
                res.push_back(message);
            }

            return res;
        }

    private:
        std::shared_ptr<elasticlient::Client> _es_client;  
};